<?php
if(is_numeric($id)) {
    require_once 'includes/getPaginationLink.php';
	$sql = "SELECT * FROM ".$tb_prefix."data WHERE m_id = $id";
	$query = $mysql->query($sql);
		if($mysql->num_rows($query)) {
				$mysql->query("UPDATE ".$tb_prefix."data SET m_viewed = m_viewed + 1, m_viewed_month = m_viewed_month + 1, m_viewed_week = m_viewed_week + 1, m_viewed_day = m_viewed_day + 1 WHERE m_id = '$id'");
				$row = $mysql->fetch_array($query);
				$song_ID 		 = $row['m_id'];
				$song_TITLE 	 = $row['m_title'];
				$m_url			= $row['m_url'];
				$singer_ID		 = $row['m_singer'];
				$singer_NAME     =  m_get_data('SINGER',$singer_ID);
				$song_URL = url_link($song_ID,$song_TITLE,'nhac-san');				
				$cat_ID		 	 =  $row['m_cat'];
				$cat_NAME     	 =  m_get_data('CAT',$cat_ID);
				
				$user_ID		 =  $row['m_poster'];
				$user_NAME	     =  m_get_data('USER',$user_ID);
				$login_ID 		 =  $_SESSION['user_id'];
				$checkLike = 0;
				//check like or unlike
                if($login_ID != '') {
                    $q = $mysql->query("SELECT * FROM ".$tb_prefix."post WHERE post_media_id = ". $song_ID . " AND post_poster_id = ". $login_ID);
                    if($mysql->num_rows($q)) {
                        $checkLike = 1;
                    } 
                }
				//sumlike
				$s = $mysql->fetch_array($mysql->query("SELECT COUNT(*) as sumLike FROM ". $tb_prefix . "post WHERE post_media_id =". $song_ID));
				$sumLike = $s['sumLike'];
                $listUser = '';
                $contentListLike = '';
                if($checkLike) {
                    $sumLike -= 1;
                    $contentListLike = "bạn";
                    $q = $mysql->query("SELECT p.post_poster_id, u.user_name  FROM ". $tb_prefix . "post AS p LEFT JOIN ". $tb_prefix . "user AS u ON p.post_poster_id = u.user_id WHERE p.post_media_id =" . $song_ID . " AND p.post_type=1 AND p.post_poster_id <> $login_ID ORDER BY post_time DESC LIMIT 0, 2");
                    if($mysql->num_rows($q)) {
                        while($row = $mysql->fetch_array($q)) {
                            $sumLike -= 1;
                            $contentListLike .= ", " . "<a href='" . url_link($row['post_poster_id'],$row['user_name'],'User') . "'>{$row['user_name']}</a>";
                        }
                    } else {
                        $contentListLike = "bạn";
                    }
                } else {
                    $q = $mysql->query("SELECT p.post_poster_id, u.user_name  FROM ". $tb_prefix . "post AS p LEFT JOIN ". $tb_prefix . "user AS u ON p.post_poster_id = u.user_id WHERE p.post_media_id =" . $song_ID . " AND p.post_type=1 ORDER BY post_time DESC LIMIT 0, 3");
                    if($mysql->num_rows($q)) {
                        while($row = $mysql->fetch_array($q)) {
                            $sumLike -= 1;
                            if($contentListLike != '')
                                $contentListLike .= ",";
                            $contentListLike .= " <a href='" . url_link($row['post_poster_id'],$row['user_name'],'User') . "'>{$row['user_name']}</a>";
                        }
                    } else {
                        $contentListLike = "";
                    }
                }
                
                if($sumLike > 0 && $contentListLike != '') {
                    $listUser = "Có " . $contentListLike . " và ";
                } elseif($contentListLike != '') {
                    $listUser = "Có " . $contentListLike . " ";
                }
                
                if($listUser == '')
                    $listUser = "Chưa có ai";
                
				if($user_NAME == '')
				{
					$user_NAME = 'other';
				}
				if(m_user_level($user_ID) == "Admin") {
					$user_NAME	= "<font color=#CC0000 >".$user_NAME."</font>";
				}
				if(m_user_level($user_ID) == "Moderator") {
					$user_NAME	= "<font color=#FF9900 >".$user_NAME."</font>";
				}				
			
				$album_ID		 =	$row['m_album'];
				$album_NAME      =  m_get_data('ALBUM',$album_ID);
				if($album_NAME == '')
				{
					$album_NAME = 'Đang cập nhật';
				}
				$song_COMMENTS	 =  $mysql->fetch_array($mysql->query("SELECT COUNT(comment_media_id) FROM ".$tb_prefix."comment WHERE comment_media_id = '".$row['m_id']."'"));
				$song_COMMENTS	 =  $song_COMMENTS[0];	
				$song_DOWNLOAD	 =  $mainURL.'/'.'download.php?id='.$song_ID.'&salt='.md5($song_ID);
	
				$song_VIEWED	 = $row['m_viewed'];
				$song_DOWNLOADED = $row['m_downloaded'];
				$song_LYRIC		 =  m_emotions_replace(($row['m_lyric'])?m_text_tidy($row['m_lyric']):'Bài hát này chưa có lời');
				$avatar	 =  $mysql->fetch_array($mysql->query("SELECT user_avatar FROM ".$tb_prefix."user WHERE user_id = '".$user_ID."'"));
				$avatar = $avatar[0];
				if($avatar == ''){
					$avatar = 'http://matxac.com/MATXAC2.jpg';
				}
				$width			 = (!$row['m_width'])?400:$row['m_width'];
				$height 		 = (!$row['m_height'])?60:$row['m_height'];
				$url_play 		 = $mainURL.'/asx.php?type=1&id='.$song_ID;
				$url_play1 = $mainURL.'/Play/1/'.$song_ID.'.xml';
				$song_BLOG		 =  "<OBJECT type='application/x-oleobject' CLASSID='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' width=$width height=62>
<param name='url' value='$mainURL/$url_play'>
<param name='EnableContextMenu' value=1>
<param name=volume VALUE=100>
<embed type='application/x-mplayer2' quality=high pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' file='$mainURL/$url_play' src='$mainURL/$url_play' WIDTH='$width' HEIGHT='$height' AutoStart='1' EnableContextMenu='1' Mute='0' ShowStatusBar='1'>
</OBJECT>";

				$singer_URL = url_link($singer_ID,$singer_NAME,'Singer');
				$cat_URL    = url_link($cat_ID,$cat_NAME,'Cat');
				$user_URL	= url_link($user_ID,$user_NAME,'User');
				$album_URL	= url_link($album_ID,$album_NAME,'Album');
				$song_URL	= url_link($song_ID,$song_TITLE,'nhac-san');
				
///// SHOW RATING

    if ($row['m_rating_total'] =='0') $current_star = 0;
    else $rater_rating = $row['m_rating'] / $row['m_rating_total'];
    // Assign star image

    if ($rater_rating <= 0  ){$star1 = "star_n"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 0.5){$star1 = "star_h"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 1  ){$star1 = "star"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 1.5){$star1 = "star"; $star2 = "star_h"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 2  ){$star1 = "star"; $star2 = "star"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 2.5){$star1 = "star"; $star2 = "star"; $star3 = "star_h"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 3  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 3.5){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star_h"; $star5 = "star_n";}
    if ($rater_rating >= 4  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star_n";}
    if ($rater_rating >= 4.5){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star_h";}
    if ($rater_rating >= 5  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star";}
    $rater_stars_img =     "<img src=\"$tpl_link/images/".$star1.".gif\">"
                        ." <img src=\"$tpl_link/images/".$star2.".gif\">"
                        ." <img src=\"$tpl_link/images/".$star3.".gif\">"
                        ." <img src=\"$tpl_link/images/".$star4.".gif\">"
                        ." <img src=\"$tpl_link/images/".$star5.".gif\">";
	$rates = $rater_stars_img."".$row['m_rating_total']." Bình chọn";
				
				include("templates/".$_SESSION['current_tpl']."/play.html");
		}
}
?>